package com.tron.md5;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Start {
    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        String s = "高联瑞";
        md5.update(s.getBytes(StandardCharsets.UTF_8));
        byte[] digest = md5.digest();
        StringBuilder hash = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            int k = digest[i] & 0xff;   //处理负数,且转换后的正数不大于255
            if (k < 16) {
                hash.append("0"); // 数字小于16时转16进制为1位，加0是为了补齐总数32位
            }
            hash.append(Integer.toString(k, 16).toUpperCase());
        }
        System.out.println(hash);
    }
}
